// pages/to-signUp/to-signUp.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formArr:[],
    snum: [],
    activityId:'',
    showSex: '',
    showAddress: '',
    sex:'男',
    address:'',
    inputVal: [],
    items: [
      { name: '男', value: '男', checked: 'true' },
      { name: '女', value: '女'}
    ],
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let formArr = JSON.parse(options.formArr)
    let index = formArr.indexOf('性别')
    let showSex = ''
    let showAddress = ''
    if (index != -1) {
      showSex = '性别'
      formArr.splice(index, 1);
    }
    let index1 = formArr.indexOf('地址')
    if (index1 != -1) {
      showAddress = '地址'
      formArr.splice(index1, 1);
    }

    console.log(options)
    console.log(formArr)
    console.log(showSex)
    console.log(showAddress)
    console.log(options.snum)
    console.log(options.activityId)
    this.setData({
      showSex: showSex,
      showAddress: showAddress,
      formArr: formArr,
      snum: JSON.parse(options.snum),
      activityId: options.activityId
    })
  },


  getInputVal: function (e) {
   
    let nowIdx = e.currentTarget.dataset.idx;//获取当前索引
    let val = e.detail.value;//获取输入的值
    let oldVal= this.data.inputVal;
    oldVal[nowIdx] = val;//修改对应索引值的内容
    this.setData({
      inputVal: oldVal
    })
  },

  radioChange: function (e) {
    this.data.sex = e.detail.value
  },

  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  getAddress(e){
    this.data.address = e.detail.value
  },

 

  comfirmSignUp(){
    if (this.data.inputVal.length < this.data.formArr.length){
      for (let i = 0; i <= (this.data.formArr.length - this.data.inputVal.length);i++){
        this.data.inputVal.push('')
      }
    }
    for (let j = 0; j < this.data.inputVal.length;j++){
      if (this.data.inputVal[j] == '' || this.data.inputVal[j] === undefined){
        wx.showModal({
          title: this.data.formArr[j]+'不能为空',
          content: '',
        })
        return
      }
    }
  
    let joinAdd = ''
    if(this.data.showAddress!=''){
      joinAdd = this.data.region.join("") + this.data.address
    }
    console.log(joinAdd)
    let joinSex = ''
    if (this.data.showSex != '') {
      joinSex = this.data.sex
    }
    console.log(joinSex)
    console.log(this.data.formArr)
    console.log(this.data.activityId)
    console.log(this.data.inputVal)
    let form = {}
    for (let i = 0; i < this.data.formArr.length;i++){
      form[this.data.formArr[i]] = this.data.inputVal[i]
    }
    console.log(form)
    wx.showLoading({
      title: '报名中',
      mask: true
    })
    db.collection('signUp-user').add({
      data: {
        activityId: this.data.activityId,
        form: form,
        createTime: db.serverDate(),
        sex: joinSex,
        address: joinAdd
      }
    })
    .then((res) => {
      wx.hideLoading()
      wx.showToast({  
        title: '报名成功',
      })
      wx.navigateBack()
      const pages = getCurrentPages()
      const prePage = pages[pages.length - 2]
      prePage.onPullDownRefresh()
    })
  }
})